import 'package:flutter/material.dart';

class Olympic13 extends StatefulWidget {
  Olympic13({Key? key, required this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _Olympic13State createState() => _Olympic13State();
}

class _Olympic13State extends State<Olympic13> {
  List<String> metalTitleList = ["国家 ", "金牌 ", "银牌 ", "铜牌 ", "总数 "];
  List<Map<String, dynamic>> metalList = [
    {
      "country": 'assets/images/olympic/13/CHN.jpg',
      "Gold": 19,
      "Silver": 11,
      "Brozen": 11,
      "Total": 41
    },
    {
      "country": 'assets/images/olympic/13/JAN.jpg',
      "Gold": 17,
      "Silver": 4,
      "Brozen": 7,
      "Total": 28
    },
    {
      "country": 'assets/images/olympic/13/USA.jpg',
      "Gold": 14,
      "Silver": 18,
      "Brozen": 15,
      "Total": 47
    },
    {
      "country": 'assets/images/olympic/13/RUS.jpg',
      "Gold": 12,
      "Silver": 15,
      "Brozen": 19,
      "Total": 46
    },
    {
      "country": 'assets/images/olympic/13/RUS.jpg',
      "Gold": 12,
      "Silver": 15,
      "Brozen": 19,
      "Total": 46
    },
    {
      "country": 'assets/images/olympic/13/RUS.jpg',
      "Gold": 12,
      "Silver": 15,
      "Brozen": 19,
      "Total": 46
    },
    {
      "country": 'assets/images/olympic/13/RUS.jpg',
      "Gold": 12,
      "Silver": 15,
      "Brozen": 19,
      "Total": 46
    },
    {
      "country": 'assets/images/olympic/13/RUS.jpg',
      "Gold": 12,
      "Silver": 15,
      "Brozen": 19,
      "Total": 46
    },
    {
      "country": 'assets/images/olympic/13/RUS.jpg',
      "Gold": 12,
      "Silver": 15,
      "Brozen": 19,
      "Total": 46
    },
    {
      "country": 'assets/images/olympic/13/RUS.jpg',
      "Gold": 12,
      "Silver": 15,
      "Brozen": 19,
      "Total": 46
    },
  ];

  void _goldSort() {
    setState(() {
      metalList.sort((left, right) => right["Gold"].compareTo(left["Gold"]));
      metalTitleList = ["国家", "金牌↓", "银牌", "铜牌", "总数"];
    });
  }

  void _silverSort() {
    setState(() {
      metalList
          .sort((left, right) => right["Silver"].compareTo(left["Silver"]));
      metalTitleList = ["国家", "金牌", "银牌↓", "铜牌", "总数"];
    });
  }

  void _brozenSort() {
    setState(() {
      metalList
          .sort((left, right) => right["Brozen"].compareTo(left["Brozen"]));
      metalTitleList = ["国家", "金牌", "银牌", "铜牌↓", "总数"];
    });
  }

  void _totalSort() {
    setState(() {
      metalList.sort((left, right) => right["Total"].compareTo(left["Total"]));
      metalTitleList = ["国家", "金牌", "银牌", "铜牌", "总数↓"];
    });
  }

  TableRow tableRow(Map<String, dynamic> data) {
    return TableRow(children: <Widget>[
      Image.asset(
        data["country"],
        width: 100.0,
        height: 60.0,
      ),
      Text(
        data["Gold"].toString(),
        style: TextStyle(fontSize: 30),
        textAlign: TextAlign.center,
      ),
      Text(
        data["Silver"].toString(),
        style: TextStyle(fontSize: 30),
        textAlign: TextAlign.center,
      ),
      Text(
        data["Brozen"].toString(),
        style: TextStyle(fontSize: 30),
        textAlign: TextAlign.center,
      ),
      Text(
        data["Total"].toString(),
        style: TextStyle(fontSize: 30),
        textAlign: TextAlign.center,
      )
    ]);
  }

  TableRow tableRowTitle(List<String> data) {
    return TableRow(children: <Widget>[
      TextButton(
          onPressed: () {
            _goldSort();
          },
          child: Text(
            data[0],
            style: TextStyle(fontSize: 20),
            textAlign: TextAlign.center,
          )),
      TextButton(
          onPressed: () {
            _goldSort();
          },
          child: Text(
            data[1],
            style: TextStyle(fontSize: 20),
            textAlign: TextAlign.center,
          )),
      TextButton(
          onPressed: () {
            _silverSort();
          },
          child: Text(
            data[2],
            style: TextStyle(fontSize: 20),
            textAlign: TextAlign.center,
          )),
      TextButton(
          onPressed: () {
            _brozenSort();
          },
          child: Text(
            data[3],
            style: TextStyle(fontSize: 20),
            textAlign: TextAlign.center,
          )),
      TextButton(
          onPressed: () {
            _totalSort();
          },
          child: Text(
            data[4],
            style: TextStyle(fontSize: 20),
            textAlign: TextAlign.center,
          )),
    ]);
  }

  tableProducer() {
    List<TableRow> tlist = <TableRow>[
      tableRowTitle(metalTitleList),
    ];
    metalList.forEach((element) {
      tlist.add(tableRow(element));
    });
    return tlist;
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the Olympic13 object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: ListView(
          // Column is also a layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug painting" (press "p" in the console, choose the
          // "Toggle Debug Paint" action from the Flutter Inspector in Android
          // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
          // to see the wireframe for each widget.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          //mainAxisAlignment: MainAxisAlignment.center,
          padding: EdgeInsets.all(0.0),
          children: <Widget>[
            Table(
              defaultVerticalAlignment: TableCellVerticalAlignment.middle,
              border: TableBorder.all(
                  color: Colors.black38, width: 0, style: BorderStyle.solid),
              children: tableProducer(),
            ),
          ],
        ),
      ),
    );
  }
}
